<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程审定</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>

      <!-- 用户列表区域 -->
      <el-table :data="courselist" border stripe>
        <el-table-column label="课程编号" prop="id" ></el-table-column>
        <el-table-column label="课程名称" prop="course_name" ></el-table-column>
        <el-table-column label="课程介绍" prop="introduce" ></el-table-column>
        <el-table-column label="幼儿要求" prop="requirement" ></el-table-column>
        <el-table-column label="状态" prop="status" ></el-table-column>
        <el-table-column label="合作单位" prop="co_name" ></el-table-column>
        <el-table-column label="基本费用" prop="teach_fee" ></el-table-column>
        <el-table-column label="材料费用" prop="material_fee" ></el-table-column>
        <el-table-column label="创建人" prop="username" ></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-circle-check"  size="mini" @click="showpassDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-circle-close" size="mini" @click="removeUserById(scope.row.id)"></el-button>
            <!-- 分配角色按钮 -->
            <!-- <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row)"></el-button>
            </el-tooltip> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
                     :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>



    <el-dialog title="审定课程" :visible.sync="passDialogVisible" width="50%" @close="passDialogClosed">
      <el-form :model="passForm" :rules="passFormRules" ref="passFormRef" label-width="70px">
        <el-form-item label="课程编号">
          <el-input v-model="passForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="基本费(元)" prop="basiccost">
          <el-input v-model="passForm.basiccost"></el-input>
        </el-form-item>
        <el-form-item label="材料费(元)" prop="materialcost">
          <el-input v-model="passForm.materialcost"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="passDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="passUserInfo">审定通过</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
    export default {
        data() {
            return {
                // 获取用户列表的参数对象
                queryInfo: {
                    // 当前的页数
                    pagenum: 1,
                    // 当前每页显示多少条数据
                    pagesize: 2
                },
                courselist: [],
                total: 0,
                ConstitutionList: [],

                passDialogVisible: false,

                passForm: {
                    id: '',
                    teach_fee: '',
                    material_fee: ''
                },

                passFormRules: {
                    teach_fee: {
                        required: true,
                        message: '请输入基本费用',
                        trigger: 'blur'
                    },
                    material_fee: {
                        required: true,
                        message: '请输入教材费用',
                        trigger: 'blur'
                    }
                }
            }
        },
        created() {
            this.getCourseList()
            this.getConstitutionList()
        },
        methods: {
            async getCourseList() {
                const { data: res } = await this.$http.get('courseByAdmin/unApproved', {
                  params: this.queryInfo
                })
                if (res.code !== 200) {
                  return this.$message.error('获取用户列表失败！')
                }
                this.courselist = res.data.data
                this.total = res.data.count
            },
            async getConstitutionList() {
              const { data: res } = await this.$http.get('cooperation/list')
              if (res.code !== 200) {
                return this.$message.error('获取用户列表失败！')
              }
              this.ConstitutionList = res.data
            },
            // 监听 pagesize 改变的事件
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getCourseList()
            },
            // 监听 页码值 改变的事件
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage
                this.getCourseList()
            },
            // 展示编辑用户的对话框
            async showpassDialog(id) {
              
            const { data: res } = await this.$http.get('courseByAdmin/'+id)
              if (res.code !== 200) {
                  return this.$message.error('获取用户信息失败！')
              }

              this.passForm = res.data

              this.passDialogVisible = true

            },

            passDialogClosed() {
                this.$refs.passFormRef.resetFields()
            },

            passUserInfo() {
              let form = new FormData()
              form.append('id',this.passForm.id)
              form.append('teach_fee',this.passForm.teach_fee)
              form.append('material_fee',this.passForm.material_fee)
              form.append('status','审定通过')

                this.$refs.passFormRef.validate(async valid => {
                    if (!valid) return
                    const {
                      data: res
                    } = await this.$http.put(
                      'courseByAdmin/status',from
                    )

                    if (res.meta.status !== 200) {
                      return this.$message.error('审定通过失败！')
                    }

                    // 关闭对话框
                    this.passDialogVisible = false
                    // 刷新数据列表
                    this.getCourseList()
                    // 提示修改成功
                    this.$message.success('审定通过')
                })
            },
            async removeUserById(id) {
                const confirmResult = await this.$confirm(
                    '此操作将不通过此课程审核, 是否继续?',
                    '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).catch(err => err)
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消删除')
                }

                const {
                    data: res
                } = await this.$http.delete('users/' + id)

                if (res.meta.status !== 200) {
                    return this.$message.error('删除用户失败！')
                }

                this.$message.success('删除用户成功！')
                this.getCourseList()
            }
        }
    }
</script>

<style lang="less" scoped>
</style>
